#include "CMainViewWidget.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QDebug>
#include "MeegoSupport.h"

/*
 Main View Item - item means Icon and text label
 */
CMainViewItem::CMainViewItem(MWidget* parent)
    : m_icon(QPixmap()), m_label("")
{
    setupUi();
}

CMainViewItem::CMainViewItem(QPixmap icon, QString label)
    : m_icon(icon), m_label(label)
{

    setupUi();
}

CMainViewItem::CMainViewItem(QPixmap icon, char* label)
    : m_icon(icon), m_label(label)
{

    setupUi();
}

void CMainViewItem::SetIcon(QPixmap& icon)
{
    m_icon = icon;
    m_widgetIcon.setPixmap(m_icon);
}

void CMainViewItem::SetLabel(QString& label)
{
    m_label = label;
    m_widgetLabel.setText(m_label);
}

void CMainViewItem::mousePressEvent ( QGraphicsSceneMouseEvent * event )
{
    //MeegoSupport::MessageBox("Clicked", m_label);
    emit clicked();
}

QSizeF CMainViewItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint)
{
    return QSizeF(150, 150);
}

void CMainViewItem::setupUi()
{
    QFont f ("Nokia Sans");
    f.setPixelSize(20);
    m_widgetLabel.setFont(f);

    //setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
    m_layout = new QGraphicsLinearLayout(Qt::Vertical, this);
    m_widgetIcon.setPixmap(m_icon);
    m_widgetIcon.setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    m_widgetLabel.setText(m_label);
    m_widgetLabel.setAlignment(Qt::AlignHCenter|Qt::AlignTop);
    m_layout->addItem(&m_widgetIcon);
    m_layout->setAlignment(&m_widgetIcon, Qt::AlignCenter);
    m_layout->addItem(&m_widgetLabel);
}

/*
 Main view Status Bar (progress bar)
 */

CMainViewStatusBar::CMainViewStatusBar(MWidget* parent)
{
    setupUi();
}

CMainViewStatusBar::CMainViewStatusBar(QString text, MWidget* parent)
{
    SetText(text);
    //setupUi();
}

void CMainViewStatusBar::SetText(QString text)
{
    m_text = text;
    m_lStatus.setText(text);
    m_progress = 0;
}

void CMainViewStatusBar::SetProgress(QString text, int progress)
{
    m_text = text;
    m_lStatus.setText(text);
    m_progress = progress;
}

QSizeF CMainViewStatusBar::sizeHint(Qt::SizeHint which, const QSizeF &constraint)
{
    return QSizeF(-1, STATUS_BAR_HEIGHT);
}

void CMainViewStatusBar::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
    //painter->setPen(Qt::white);
    //painter->setBackground(QBrush("#88be67"));
    //qDebug() << "parentWidget()->boundingRect().width()" << parentWidget()->boundingRect().width() << "boundingRect().height()" << boundingRect().height();
    painter->fillRect(QRectF(0, 0, parentWidget()->boundingRect().width(), size().height()), QBrush("#88be67"));
    painter->setFont(QFont("Nokia Sans", 18));

    painter->drawText(QPointF(20, 38 - 9), m_text);

    if(m_progress > 0)
    {
        DrawProgressBar(painter, m_progress);
    }
}

void CMainViewStatusBar::setupUi()
{
    QFont f ("Nokia Sans");
    f.setPixelSize(18);
    m_lStatus.setColor(Qt::white);
    m_lStatus.setFont(f);
    m_pxmProgressBg.setStyleName("ProgressBarBg");
    setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    mainLayout = new MLayout(this);
    mainLayout->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    landscapePolicy = new MLinearLayoutPolicy(mainLayout, Qt::Horizontal);
    portraitPolicy = new MLinearLayoutPolicy(mainLayout, Qt::Vertical);
    mainLayout->setLandscapePolicy(landscapePolicy);
    mainLayout->setPortraitPolicy(portraitPolicy);

    //QPixmap p = QPixmap(":/IconAdd/9patch_syncbar_bkgd.9.png").copy(.scaled(500, 10, Qt::IgnoreAspectRatio);
    //m_pxmProgressBg.setPixmap(p);

    landscapePolicy->addItem(&m_lStatus, Qt::AlignTop);
    portraitPolicy->addItem(&m_lStatus, Qt::AlignCenter);

    landscapePolicy->addItem(&m_pxmProgressBg, Qt::AlignLeft|Qt::AlignVCenter);
    portraitPolicy->addItem(&m_pxmProgressBg, Qt::AlignLeft|Qt::AlignVCenter);
}

void CMainViewStatusBar::DrawProgressBar( QPainter * painter, int m_progress)
{
    int width = parentWidget()->boundingRect().width();
    painter->setPen(Qt::white);
    //painter->setRenderHint(QPainter::Antialiasing);

    QPainterPath round_rect;
    QPainterPath filled_rect;


    round_rect.addRoundedRect(QRectF(180, 38 / 2 - 5, width - 200, 10), 5, 10);
    //painter->setClipPath(&round_rect);
    painter->fillPath(round_rect, QBrush(Qt::white));

    int filled_width = (qreal)((qreal)m_progress / (qreal)100) * (width - 200);
    filled_rect.addRoundedRect(QRectF(180, 38 / 2 - 5, filled_width - 5, 10), 5, 10);
    filled_rect.addRect(QRectF(180 + filled_width - 10, 38 / 2 - 5, 5, 10));
    filled_rect.setFillRule(Qt::WindingFill);
    painter->setPen("#8bb572");
    painter->drawPath(filled_rect);
    painter->fillPath(filled_rect, QBrush("#cbffab"));

}

CMainViewTitleWidget::CMainViewTitleWidget(MWidget* parent)
{
    setObjectName("MainViewTitleWidget");
    setupUi();
}

QSizeF CMainViewTitleWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint)
{
    return QSizeF(-1, STATUS_BAR_HEIGHT);
}

void CMainViewTitleWidget::setupUi()
{
    setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    mainLayout = new QGraphicsLinearLayout(Qt::Horizontal, this);
    m_iwIcon.setPixmap(QPixmap(":/IconAdd/btn_full_logo_port_default.png"));
    m_iwIcon.setMaximumHeight(STATUS_BAR_HEIGHT);
    m_ciControl.setPixmap(QPixmap(":/IconAdd/general_stop.png"));

    mainLayout->addItem(&m_iwIcon);
    mainLayout->setAlignment(&m_iwIcon, Qt::AlignLeft|Qt::AlignVCenter);
    mainLayout->addItem(&m_ciControl);
    mainLayout->setAlignment(&m_ciControl, Qt::AlignRight|Qt::AlignVCenter);
}

/*
 * Main View Widget
 *
 */

CMainViewWidget::CMainViewWidget(MWidget* parent)
{
    setObjectName("MainViewWidget");
    setupUi();
}

void CMainViewWidget::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
    painter->rotate(90);
    //p.transformed()
    painter->drawPixmap(QPointF(38, -260), QPixmap(":/IconAdd/ic_drawnline_port.png").copy(0, 0, 290, 3));
    //painter->drawPixmap(QPointF(-260, 0), QPixmap(":/IconAdd/ic_drawnline_port.png"));
    //painter->setPen("#d4d4d4");
    //painter->drawLine(260, 38, 260, 38 + 278);
    //qDebug() << "Size of centralwidget: " << boundingRect();
}

void CMainViewWidget::setupUi()
{
    setContentsMargins(0, 0, 0, 0);
    mainLayout = new MLayout(this);
    mainLayout->setSizePolicy(QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored));
    verticalPolicy = new MGridLayoutPolicy(mainLayout);
    horizontalPolicy = new MGridLayoutPolicy(mainLayout);
    mainLayout->setLandscapePolicy(horizontalPolicy);
    mainLayout->setPortraitPolicy(verticalPolicy);
    //mainLayout->setHorizontalSpacing(100);
    //mainLayout->setVerticalSpacing(30);

    m_iNewNote = new CMainViewItem(QPixmap(":/IconAdd/btn_newnote_default.png"), QString("New note"));
    m_iNewNote->setContentsMargins(70, 20, 20, 0);


    m_iNotebooks = new CMainViewItem(QPixmap(":/IconAdd/btn_notebooks_default.png"), QString("Notebooks"));
    m_iNotebooks->setContentsMargins(100, 20, 0, 0);

    m_iNotes = new CMainViewItem(QPixmap(":/IconAdd/btn_allnotes_default.png"), QString("All notes"));
    m_iNotes->setContentsMargins(100, 20, 0, 0);

    m_iSnapshot = new CMainViewItem(QPixmap(":/IconAdd/btn_camera_default.png"), QString("Snapshot"));
    m_iSnapshot->setContentsMargins(70, 0, 20, 0);

    m_iTags = new CMainViewItem(QPixmap(":/IconAdd/btn_tags_default.png"), QString("Tags"));
    m_iTags->setContentsMargins(100, 0, 0, 0);

    m_iSearches = new CMainViewItem(QPixmap(":/IconAdd/btn_searches_default.png"), QString("Searches"));
    m_iSearches->setContentsMargins(100, 0, 0, 0);

    m_statusBar = new CMainViewStatusBar("Welcome");
    m_statusBar->SetProgress("Welcome", 20);
    m_statusBar->setMaximumHeight(38);
    setupHorizontalPolicy();
}

void CMainViewWidget::setupVerticalPolicy()
{

}

void CMainViewWidget::setupHorizontalPolicy()
{
    horizontalPolicy->addItem(&m_titleWidget, 1, 1, 1, 3);
    m_titleWidget.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    horizontalPolicy->addItem(m_iNewNote, 2, 1, Qt::AlignCenter);
    horizontalPolicy->addItem(m_iNotebooks, 2, 2, Qt::AlignCenter);
    horizontalPolicy->addItem(m_iNotes, 2, 3, Qt::AlignCenter);

    horizontalPolicy->addItem(m_iSnapshot, 3, 1, Qt::AlignCenter);
    horizontalPolicy->addItem(m_iTags, 3, 2, Qt::AlignCenter);
    horizontalPolicy->addItem(m_iSearches, 3, 3, Qt::AlignCenter);
    horizontalPolicy->addItem(m_statusBar, 4, 1, 1, 3, Qt::AlignBottom);
}
